from appengine_django.models import BaseModel
from google.appengine.ext import db

# Create your models here.

from google.appengine.api import users

class SCUser(db.Model):
    email_id = db.StringProperty(multiline=False)
    user = db.UserProperty(required=True)# do we need stringProperty?
    role = db.StringProperty(choices=('Admin', 'ActonCreator', 'None'), default = 'None') #based on http://code.google.com/appengine/articles/modeling.html
    
class PrimaryCategory(db.Model):
    name = db.StringProperty(multiline=False)
    relativeWeight = db.RatingProperty()# value from 0 to hundred
     
class Category(db.Model):
    name = db.StringProperty(multiline=False)
    parentCategory = db.ReferenceProperty(PrimaryCategory)#
    
class Skills(db.Model):
    name = db.StringProperty(multiline=False)
    
class Tags(db.Model):
    name = db.StringProperty(multiline=False)
 
class Priority(db.Model):
     name = db.StringProperty(multiline=False)
     rank = db.IntegerProperty()
      
class Status(db.Model):
    name = db.StringProperty(multiline=False)
     

 
class Acton(db.Model): #root of all troubles =)#see: http://code.google.com/appengine/articles/modeling.html
    creator = db.ReferenceProperty(SCUser)
    creationTime = db.DateTimeProperty(auto_now_add=True)
    category = db.ListProperty(db.Key)
    priority = db.ReferenceProperty(Priority)
    tags = db.ListProperty(db.Key) #list of tags
    title = db.StringProperty(multiline=False)
    description = db.TextProperty()
    #self.response.out.write(cgi.escape(self.request.get('content')))----this command useful in sanitizing
    skills = db.ListProperty(db.Key)
    eta = db.DateTimeProperty()#do we need timestamp or will date suffice?
    timeNeeded = db.TimeProperty()
    coordinators = db.ListProperty(db.Key)
    subscribers = db.ListProperty(db.Key)
    actors = db.ListProperty(db.Key)
    relatedTo = db.ListProperty(db.Key)
    status = db.ReferenceProperty(Status)#,choices=('new', 'assigned', 'closed', 'modified', 'verified', 'deleted'), default='closed')#is ListProperty more Appropriate?
    slug = db.StringProperty(multiline=False)
    #removed'discussion'
     
class Post(db.Model):
    poster = db.ReferenceProperty(SCUser)
    content = db.TextProperty()
    timestamp = db.DateTimeProperty(auto_now_add=True)
    #http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#DateTimeProperty
    #see auto_add_now and auto_add subtle differences
    acton = db.ReferenceProperty(Acton)
    deleted=db.BooleanProperty(bool)
#removed Discussion class
class Log(db.Model):
    by = db.ReferenceProperty(SCUser)
    on = db.Key()
	#    on = db.IntegerProperty#will be the key of certain entry
    #removed 'type'- using the key we can extract the type
    details = db.TextProperty()

#class Search(db.model):
#    keyword = db.TextProperty()
#    records = db.ListProperty(long)
    
#####################################sample methods###############
#####
#Method1: To add a tag to an acton
#
#tag1 = ActonTags.gql("WHERE name = 'tag1'").get()
#if tag1.key() not in acton1.category: # here tag1 and acton1 are entitities of the kind ActonTags and Acton respectively
#  acton1.category.append(tag1.key())
#  acton1.put()
#####
#Method2: to list out all the posts of an acton
#
#posts = acton1.post_set
#for post in posts:# lists out all the posts referrring to acton1(entity) of the Acton(kind)
#    ...
#####
#Method3: to list out all actons under a category(a subcategory to be precise)
#[This can be made into a helper method as suggested in http://code.google.com/appengine/articles/modeling.html
#
#key = category1.key()
#actons = Acton.gql("WHERE category = :1", key)
#for acton in actons:
#    ...
#####
#Method4a: list out all the actors for an acton
#actors = acton1.actors
#for actor in actors:
#    ...
#Method4b:
#key = user1.key()
#actons=acton.gql("WHERE actor= :1", key)
#####
##################################################################
